package com.gin.kotlin.no_gay_alliance.adapter.follow

import android.content.Context
import android.view.View
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.gin.kotlin.no_gay_alliance.R
import com.gin.kotlin.no_gay_alliance.activity.ReplyListActivity
import com.gin.kotlin.no_gay_alliance.activity.UserCenterActivity
import com.gin.kotlin.no_gay_alliance.argument.ReplyListArgument
import com.gin.kotlin.no_gay_alliance.argument.ReplyListArgument.Type
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingRecyclerViewAdapter
import com.gin.kotlin.no_gay_alliance.base.GlideModule.Companion.CROSS_FADE
import com.gin.kotlin.no_gay_alliance.base.MyHandler
import com.gin.kotlin.no_gay_alliance.base.TextViewTarget
import com.gin.kotlin.no_gay_alliance.databinding.ItemFollowDynamicsBinding
import com.gin.kotlin.no_gay_alliance.utils.TimeUtils.Companion.zdtIntoTextView
import com.gin.nga.enums.FollowStatusType
import com.gin.nga.response.follow.FollowStatus

class FollowDynamicsAdapter(context: Context, handler: MyHandler) : AbstractBindingRecyclerViewAdapter<ItemFollowDynamicsBinding, FollowStatus>(context, handler),
    View.OnClickListener {
    override fun onBindViewHolder(holder: ViewHolder<ItemFollowDynamicsBinding>, position: Int, b: ItemFollowDynamicsBinding, dataModel: FollowStatus) {
        b.tvUsername.text = dataModel.user?.username
        b.tvUsername.setOnClickListener { UserCenterActivity.startActivity(context, dataModel.userId!!) }

        b.tvDynamicsType.text = when (dataModel.type) {
            FollowStatusType.TOPIC -> "发布主题"
            FollowStatusType.REPLY -> "发布回复"
            FollowStatusType.FAV -> "收藏"
            null -> "未知类型"
        }

        val defaultTextColor = getAttrColor(R.attr.textColor)
        dataModel.timestamp?.also { zdtIntoTextView(context, it, b.tvTimestamp, defaultTextColor) }

        // 图标大小（dp）
        val size = (48 * context.resources.displayMetrics.density).toInt()

        // 加载版面头像
        Glide.with(context)
            .load(dataModel.user?.getAvatars()?.get(0))
            .apply(RequestOptions().circleCrop())
            .error(R.drawable.ic_person_18)
            .transition(DrawableTransitionOptions.withCrossFade(CROSS_FADE))
            .into(TextViewTarget(b.tvUsername, 0, size, size))

        b.tvTopicTitle.text = dataModel.topic?.title
        b.tvTopicTitle.tag = dataModel
        b.tvTopicTitle.setOnClickListener(this)

        configTopicTitleFont(b.tvTopicTitle, dataModel.topic?.getTopicStatus()?:listOf())
    }

    override fun onClick(v: View?) {
        val status = v?.tag as? FollowStatus
        val topicId = status?.topicId?.takeIf { it > 0 }
        val replyId = status?.replyId?.takeIf { it > 0 }

        if (replyId != null) {
            // 打开回复
            ReplyListActivity.startActivity(context, ReplyListArgument(Type.SINGLE, replyId = replyId))
        } else {
            // 打开主题
            ReplyListActivity.startActivity(context, ReplyListArgument(Type.LIST, topicId = topicId))
        }
    }
}